package com.lepu.app.fun.mother_circle.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.BitmapUtil;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.MotherPubContentActivity;

public class MotherPubAddImageAdapter extends BaseAdapter {

	private static final int MAX_COUNT = 3;
	private LayoutInflater mLayoutInflater = null;
	private MotherPubContentActivity mContext = null;
	@SuppressWarnings("rawtypes")
	private ArrayList mArrayList = null;

	@SuppressWarnings("rawtypes")
	public MotherPubAddImageAdapter(MotherPubContentActivity context,
			ArrayList arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 1;
		if (mArrayList != null && mArrayList.size() > 0) {

			if (mArrayList.size() >= MAX_COUNT) {
				size = MAX_COUNT;
			} else {
				size += mArrayList.size();
			}

		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.mother_pub_add_griditem, null);

			holder = new ViewHolder();

			ImageView dataImageView = (ImageView) convertView
					.findViewById(R.id.dataImageView);
			ImageView deleteImageView = (ImageView) convertView
					.findViewById(R.id.deleteImageView);

			holder.dataImageView = dataImageView;
			holder.deleteImageView = deleteImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (mArrayList == null || mArrayList.size() == 0) {
			UniversalImageLoadTool.disPlayNoCache(ImageUtilBase
					.getDrawableImageUrl(R.drawable.mother_pub_add),
					holder.dataImageView, R.drawable.mother_pub_add);
			holder.deleteImageView.setVisibility(View.GONE);

			holder.dataImageView.setOnClickListener(mAddImageClickListener);
		} else {

			if (mArrayList.size() != MAX_COUNT) {
				if (position == getCount() - 1) {
					holder.deleteImageView.setVisibility(View.GONE);

					UniversalImageLoadTool.disPlayNoCache(ImageUtilBase
							.getDrawableImageUrl(R.drawable.mother_pub_add),
							holder.dataImageView, R.drawable.mother_pub_add);
					holder.dataImageView
							.setOnClickListener(mAddImageClickListener);
				} else {
					String imageUrl = (String) mArrayList.get(position);
					holder.deleteImageView.setVisibility(View.VISIBLE);
					holder.deleteImageView
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									mContext.removeImage(position);
								}
							});

//					UniversalImageLoadTool.disPlayNoCache(
//							ImageUtilBase.getFileImageUrl(imageUrl),
//							holder.dataImageView, R.drawable.default_icon_small);
					
					Bitmap bitmap = ImageUtilBase.getBitmap(imageUrl,300,300);
					holder.dataImageView.setImageBitmap(BitmapUtil.reviewPicRotate(bitmap, imageUrl));
					
					//用这个不行，似乎照片显示重复
//					LocalImageLoader imageloader = LocalImageLoader.getInstance(mContext);
//					imageloader.disPlay(ImageUtilBase.getFileImageUrl(imageUrl), new RotateImageViewAware(holder.dataImageView,
//							imageUrl), R.drawable.default_icon_small);
					
					holder.dataImageView
							.setOnClickListener(new View.OnClickListener() {

								@Override
								public void onClick(View v) {
									mContext.seeImage(position);
								}
							});
				}
			} else {
				String imageUrl = (String) mArrayList.get(position);
				holder.deleteImageView.setVisibility(View.VISIBLE);
				holder.deleteImageView
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								mContext.removeImage(position);
							}
						});
				
//				UniversalImageLoadTool.disPlayNoCache(
//						ImageUtilBase.getFileImageUrl(imageUrl),
//						holder.dataImageView, R.drawable.default_icon_small);

//				LocalImageLoader imageloader = LocalImageLoader.getInstance(mContext);
//				imageloader.disPlay(ImageUtilBase.getFileImageUrl(imageUrl), new RotateImageViewAware(holder.dataImageView,
//						imageUrl), R.drawable.default_icon_small);

				Bitmap bitmap = ImageUtilBase.getBitmap(imageUrl,300,300);
				holder.dataImageView.setImageBitmap(BitmapUtil.reviewPicRotate(bitmap, imageUrl));
				
				holder.dataImageView
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								mContext.seeImage(position);
							}
						});
			}
		}

		return convertView;
	}

	private View.OnClickListener mAddImageClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			mContext.addImage();
		}
	};

	private static class ViewHolder {
		ImageView dataImageView;
		ImageView deleteImageView;
	}
}
